﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace WanPiJie.Img
{
	/// <summary>
	/// Http载入器
	/// </summary>
	internal class HttpLoader
	{
		/// <summary>
		/// Http请求处理
		/// </summary>
		private HttpWebRequest request;

		/// <summary>
		/// Http请求处理
		/// </summary>
		public HttpWebRequest Request
		{
			get { return request; }
		}

		/// <summary>
		/// Http响应处理
		/// </summary>
		private HttpWebResponse response;

		/// <summary>
		/// Http响应处理
		/// </summary>
		public HttpWebResponse Response
		{
			get { return response; }
		}

		/// <summary>
		/// Http载入器的构造
		/// </summary>
		public HttpLoader()
		{
			//空实现
		}

		/// <summary>
		/// Http载入器的构造
		/// </summary>
		/// <param name="uri">指定的uri</param>
		public HttpLoader(Uri uri)
		{
			this.request = (HttpWebRequest)WebRequest.Create(uri);
		}

		/// <summary>
		/// Http载入器的构造
		/// </summary>
		/// <param name="url">指定的url</param>
		public HttpLoader(string url)
		{
			this.request = (HttpWebRequest)WebRequest.Create(url);
		}

		/// <summary>
		/// Get请求
		/// </summary>
		private void GetRequest()
		{
			this.request.Method = "GET";
			this.response = (HttpWebResponse)this.request.GetResponse();
		}

		/// <summary>
		/// Get请求
		/// </summary>
		private void GetRequest(Uri uri)
		{
			this.request = (HttpWebRequest)WebRequest.Create(uri);
			this.GetRequest();
		}

		private void GetRequest(string url)
		{
			this.GetRequest(new Uri(url));
		}

		/// <summary>
		/// Post请求
		/// </summary>
		private void PostRequest()
		{
			//暂不支持
		}

		/// <summary>
		/// 保存响应流至内存中
		/// </summary>
		/// <returns>响应的内存流</returns>
		public MemoryStream SaveResponseToMemory()
		{
			//开始请求
			this.GetRequest();
			return this.StuffToMemoryStream();
		}

		/// <summary>
		/// 填充至内存流
		/// </summary>
		/// <returns>响应的内存流</returns>
		private MemoryStream StuffToMemoryStream()
		{
			//每次从网络流中读取1024个字节至内存流
			byte[] buffer = new byte[1024];
			MemoryStream ms = new MemoryStream();
			Stream netStream = this.response.GetResponseStream();
			int count = netStream.Read(buffer, 0, buffer.Length);
			while (count > 0)
			{
				ms.Write(buffer, 0, count);
				count = netStream.Read(buffer, 0, buffer.Length);
			}
			ms.Flush();
			ms.Seek(0, SeekOrigin.Begin);
			netStream.Close();
			this.response.Close();
			return ms;
		}

		/// <summary>
		/// 保存响应流至内存中
		/// </summary>
		/// <param name="uri">请求的URI</param>
		/// <returns>响应的内存流</returns>
		public MemoryStream SaveResponseToMemory(Uri uri)
		{
			this.GetRequest(uri);
			return this.StuffToMemoryStream();
		}

		/// <summary>
		/// 保存响应流至内存中
		/// </summary>
		/// <param name="url">请求的url</param>
		/// <returns>响应的内存流</returns>
		public MemoryStream SaveResponseToMemory(string url)
		{
			this.GetRequest(url);
			return this.StuffToMemoryStream();
		}
	}
}
